/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package brickroadfitness;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.sql.*;

public class EditEquip extends JFrame implements ActionListener
{
    private JLabel ID;
    private JLabel editequip;
    private JLabel header;
    private JLabel idtext;
    private JButton cancel;
    private JPanel editpanel;
    private JLabel name;
    private JTextField nametext;
    private JLabel price;
    private JTextField priceText;
    private JButton save;
    private JLabel status;
    private JOptionPane savemsg;
    private JOptionPane cancelmsg;
    private JRadioButton func, broken;
    private ButtonGroup buttongrp;
    private String equipID;
    public PreparedStatement pstmt;
    public Statement stmt1, stmt2;
    public ResultSet rset;
    public String query1, query2;
    public int n;
    cnd a = new cnd();


    public EditEquip(String equipID)
    {
        super("Brickroad Fitness - Edit Equipment Info");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(new Color(255, 255, 102));
        setResizable(false);
        setVisible(true);
        //setLocationRelativeTo(null);
        setForeground(new Color(255, 255, 102));
        this.equipID=equipID;
        initComponents(equipID);
    }

    private void initComponents(String equipID) {

        header = new JLabel();
        editpanel = new JPanel();
        editequip = new JLabel();
        ID = new JLabel();
        name = new JLabel();
        price = new JLabel();
        status = new JLabel();
        idtext = new JLabel();
        nametext = new JTextField();
        priceText = new JTextField();
        save = new JButton();
        cancel = new JButton();
        savemsg = new JOptionPane();
        cancelmsg = new JOptionPane();
        func = new JRadioButton();
        broken = new JRadioButton();
        buttongrp = new ButtonGroup();

        buttongrp.add(func);
        buttongrp.add(broken);

        save.addActionListener(this);
        cancel.addActionListener(this);

        header.setIcon(new ImageIcon(getClass().getResource("/PRIMER.jpg"))); // NOI18N
        header.setText("Brickroad Fitness");

        editpanel.setBackground(new Color(255, 255, 102));
        editpanel.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 5));

        editequip.setFont(new Font("Arial", 3, 18)); // NOI18N
        editequip.setText("Edit Equipment Info");

        ID.setFont(new Font("Tempus Sans ITC", 1, 16)); // NOI18N
        ID.setText("Equipment ID:");

        name.setFont(new Font("Tempus Sans ITC", 1, 16)); // NOI18N
        name.setText("Equipment Name:");

        price.setFont(new Font("Tempus Sans ITC", 1, 16)); // NOI18N
        price.setText("Price:");

        status.setFont(new Font("Tempus Sans ITC", 1, 16)); // NOI18N
        status.setText("Condition:");

        idtext.setFont(new Font("Tahoma", 0, 14)); // NOI18N
        //idtext.setText("1");

        nametext.setFont(new Font("Tahoma", 0, 14)); // NOI18N
        //nametext.setText("Proteus MTM 7520 Motorized Treadmill");

        priceText.setFont(new Font("Tahoma", 0, 14)); // NOI18N
        //priceText.setText("75 500");
        
        func.setBackground(new Color(255, 255, 102));
        func.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        func.setText("Functional");

        broken.setBackground(new Color(255, 255, 102));
        broken.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        broken.setText("Broken");
        broken.setSelected(true);

        save.setFont(new Font("Century Gothic", 1, 18)); // NOI18N
        save.setText("SAVE");
        save.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        cancel.setFont(new Font("Century Gothic", 1, 18)); // NOI18N
        cancel.setText("CANCEL");
        cancel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        try{
                       a.connect();

                       System.out.println(ID);
                       query1 = "SELECT * FROM equipment WHERE eqID = "+equipID;

                       stmt1 = a.con1.createStatement();
                       rset = stmt1.executeQuery(query1);
                       rset.next();

                       idtext.setText(equipID);
                       nametext.setText(rset.getString("eqName"));
                       priceText.setText(rset.getString("eqPrice"));

                       query1 = "SELECT eqCondition FROM equipment WHERE eqID = "+equipID;

                       stmt1 = a.con1.createStatement();
                       rset = stmt1.executeQuery(query1);
                       rset.next();
                       
                       if(rset.getString("eqCondition").equals("Functional"))
                            func.setSelected(true);
                       else
                            broken.setSelected(true);
                       a.disconnect();


        }
        catch(Exception e){}


        GroupLayout editpanelLayout = new GroupLayout(editpanel);
        editpanel.setLayout(editpanelLayout);
        editpanelLayout.setHorizontalGroup(
            editpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(editpanelLayout.createSequentialGroup()
                .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(editpanelLayout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(editequip))
                    .addGroup(editpanelLayout.createSequentialGroup()
                        .addGap(134, 134, 134)
                        .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(ID)
                            .addComponent(name)
                            .addComponent(price)
                            .addComponent(status))
                        .addGap(23, 23, 23)
                        .addGroup(editpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(editpanelLayout.createSequentialGroup()
                                .addComponent(func)
                                .addGap(18, 18, 18)
                                .addComponent(broken))
                            .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(nametext, GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                                .addComponent(idtext, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE))
                                .addComponent(priceText, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)))
                    .addGroup(editpanelLayout.createSequentialGroup()
                        .addGap(275, 275, 275)
                        .addComponent(save, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(cancel, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(150, Short.MAX_VALUE))
        );
        editpanelLayout.setVerticalGroup(
            editpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(editpanelLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(editequip)
                .addGap(18, 18, 18)
                .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(ID)
                    .addComponent(idtext, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(name)
                    .addComponent(nametext, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(price)
                    .addComponent(priceText, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(status)
                    .addComponent(func)
                    .addComponent(broken))
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(editpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(cancel, GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                    .addComponent(save, GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE))
                .addContainerGap())
        );

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                .addComponent(editpanel, GroupLayout.Alignment.LEADING, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(header, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 890, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(header)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editpanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    public void actionPerformed(ActionEvent e)
    {
        Object event = new Object();
        event = e.getSource();

        if (event == save)
        {
            if( !(nametext.getText().isEmpty())
                && !(priceText.getText().isEmpty())
                && buttongrp.getSelection() != null)
            {
                int ans;
                ans = savemsg.showConfirmDialog(this, "Do you really want to save and apply the changes you've done?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                if (ans == 0)
                {
                    try{
                        a.connect();
                        stmt2 = a.con2.createStatement();
                        query2 = "UPDATE `equipment`" +
                        "SET `eqName` = \"" + nametext.getText() + "\", `eqPrice` = \"" + priceText.getText()+
                        "\" WHERE `eqID` = " + equipID;
                            stmt2.executeUpdate(query2);

                         if (func.isSelected())
                         {
                             stmt2 = a.con2.createStatement();
                             query2 = "UPDATE `equipment`" +
                                "SET `eqCondition` = 'Functional' WHERE `eqID` = " + equipID;
                            stmt2.executeUpdate(query2);
                         }
                         else if (broken.isSelected())
                         {
                             stmt2 = a.con2.createStatement();
                             query2 = "UPDATE `equipment`" +
                                "SET `eqCondition` = 'Broken' WHERE `eqID` = " + equipID;
                            stmt2.executeUpdate(query2);
                         }
                         savemsg.showMessageDialog(this, "Save successful!", "Save", JOptionPane.INFORMATION_MESSAGE);
                         System.out.println("Edit Successful");
                         dispose();
                         new EquipInfo(equipID);
                         a.disconnect();
                     }
                     catch(Exception z){
                         System.out.println("Error copying records from course table. " + z.getMessage());
                     }
                }
            }
            else
                savemsg.showMessageDialog(this, "Incomplete fields", "Error", JOptionPane.ERROR_MESSAGE);
        }
        else if(event == cancel)
        {
            int ans;
            ans = cancelmsg.showConfirmDialog(this, "Are you sure you want to cancel? All unsaved information will be lost.", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            if(ans == 0)
            {
                dispose();
                new EquipInfo(equipID);
            }
        }
    }
}
